package xcu.lxj.nettychat.chat.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xcu.lxj.nettychat.chat.domain.vo.request.ChatMessageMemberReq;
import xcu.lxj.nettychat.chat.domain.vo.request.GroupAddReq;
import xcu.lxj.nettychat.chat.domain.vo.request.admin.AdminAddReq;
import xcu.lxj.nettychat.chat.domain.vo.request.admin.AdminRevokeReq;
import xcu.lxj.nettychat.chat.domain.vo.request.member.MemberAddReq;
import xcu.lxj.nettychat.chat.domain.vo.request.member.MemberDelReq;
import xcu.lxj.nettychat.chat.domain.vo.request.member.MemberExitReq;
import xcu.lxj.nettychat.chat.domain.vo.request.member.MemberReq;
import xcu.lxj.nettychat.chat.domain.vo.response.ChatMemberListResp;
import xcu.lxj.nettychat.chat.domain.vo.response.ChatMemberResp;
import xcu.lxj.nettychat.chat.domain.vo.response.GroupBaseInfoResp;
import xcu.lxj.nettychat.chat.domain.vo.response.MemberResp;
import xcu.lxj.nettychat.chat.service.inter.GroupMemberService;
import xcu.lxj.nettychat.chat.service.inter.RoomAppService;
import xcu.lxj.nettychat.common.domain.vo.request.CursorPageBaseReq;
import xcu.lxj.nettychat.common.domain.vo.request.IdReqVO;
import xcu.lxj.nettychat.common.domain.vo.response.ApiResult;
import xcu.lxj.nettychat.common.domain.vo.response.CursorPageBaseResp;
import xcu.lxj.nettychat.common.domain.vo.response.IdRespVO;
import xcu.lxj.nettychat.common.utils.RequestHolder;

import java.util.List;
import java.util.Objects;

/**
 * @Author XinJian Lei
 * @Date 2024/10/2 20:07
 * @Version 1.0
 */

@RestController
@Tag(name = "聊天室相关接口")
@Slf4j
public class RoomController {
    @Autowired
    private RoomAppService roomService;
    @Autowired
    private GroupMemberService groupMemberService;

    @PostMapping("/public/group")
    @Operation(summary="群组详情")
    public ApiResult<MemberResp> groupDetail(@Valid @RequestBody IdReqVO request) {
        Long uid = RequestHolder.get().getUid();
        return ApiResult.success(roomService.getGroupDetail(uid, request.getId()));
    }

    @PostMapping("/public/group/member/page")
    @Operation(summary="群成员列表")
    public ApiResult<CursorPageBaseResp<ChatMemberResp>> getMemberPage(@Valid @RequestBody MemberReq request) {
        return ApiResult.success(roomService.getMemberPage(request));
    }

    @PostMapping("/group/member/list")
    @Operation(summary="房间内的所有群成员列表-@专用")
    public ApiResult<List<ChatMemberListResp>> getMemberList(@Valid @RequestBody ChatMessageMemberReq request) {
        return ApiResult.success(roomService.getMemberList(request));
    }

    @DeleteMapping("/group/member")
    @Operation(summary="移除成员")
    public ApiResult<Void> delMember(@Valid @RequestBody MemberDelReq request) {
        Long uid = RequestHolder.get().getUid();
        roomService.delMember(uid, request);
        return ApiResult.success();
    }

    @DeleteMapping("/group/member/exit")
    @Operation(summary="退出群聊")
    public ApiResult<Boolean> exitGroup(@Valid @RequestBody MemberExitReq request) {
        Long uid = RequestHolder.get().getUid();
        groupMemberService.exitGroup(uid, request);
        return ApiResult.success();
    }

    @PostMapping("/group")
    @Operation(summary="新增群组")
    public ApiResult<IdRespVO> addGroup(@Valid @RequestBody GroupAddReq request) {
        Long uid = RequestHolder.get().getUid();
        Long roomId = roomService.addGroup(uid, request);
         return ApiResult.success(IdRespVO.id(roomId));
    }

    @PostMapping("/group/member")
    @Operation(summary="邀请好友")
    public ApiResult<Void> addMember(@Valid @RequestBody MemberAddReq request) {
        Long uid = RequestHolder.get().getUid();
        roomService.addMember(uid, request);
        return ApiResult.success();
    }

    @PutMapping("/group/admin")
    @Operation(summary="添加管理员")
    public ApiResult<Boolean> addAdmin(@Valid @RequestBody AdminAddReq request) {
        Long uid = RequestHolder.get().getUid();
        groupMemberService.addAdmin(uid, request);
        return ApiResult.success();
    }

    @DeleteMapping("/group/admin")
    @Operation(summary="撤销管理员")
    public ApiResult<Boolean> revokeAdmin(@Valid @RequestBody AdminRevokeReq request) {
        Long uid = RequestHolder.get().getUid();
        groupMemberService.revokeAdmin(uid, request);
        return ApiResult.success();
    }

    //TODO 获取群聊列表
    @PostMapping("/group/list")
    @Operation(summary = "获取群聊列表")
    public ApiResult<CursorPageBaseResp<GroupBaseInfoResp>> getGroupBaseList(@Valid @RequestBody CursorPageBaseReq req){

        Long uid = RequestHolder.get().getUid();
        return ApiResult.success(groupMemberService.getGroupBaseInfoList(req,uid));
    }

}
